package edu.lnu.project.timeeditapp.widget;

import com.lnu.project.timeeditapp.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

public class WeekWidgetProvider extends AppWidgetProvider {
	
	/**
	 * This method is called every 30 min specifi in the widget_provider.xml
	 * This method is also called on phone reboot
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		//We will always only have one instance of the widget
		int appWidgetId = appWidgetIds[appWidgetIds.length-1];
		RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
		
		remoteViews.setOnClickPendingIntent(R.id.buttonUpdateWeekWidgetView, buildUpdateButtonPendingIntent(context, appWidgetId));
		remoteViews.setOnClickPendingIntent(R.id.buttonGoToListViewFromWidget, buildListViewButtonPendingIntent(context, appWidgetId));
		
		pushWidgetUpdate(context, remoteViews);
	}
	
	/**
	 * Update widget list view
	 * @param context
	 * @param appWidgetId
	 * @return the a remoteView
	 */
	@SuppressWarnings("deprecation")
	public static RemoteViews updateWidgetListView(Context context, int appWidgetId) {
	    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.timeeditwidgetweek);

	    // RemoteViews Service needed to provide adapter for ListView
	    Intent intent = new Intent(context, WeekWidgetService.class);
	    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
	    intent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis())); // This is needed to make this intent different from its previous intents
	    
	    /* Control  what is added to the widget */
	    // intent.putExtra(.....); // TODO Add information so we can control what the widget is showing
	    
	    // Setting adapter to listview of the widget
	    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidgetWeek, intent);
		
		return remoteViews;
	}

	/**
	 * Build the preference button pending intent
	 * @param context
	 * @return the pending intent
	 */
	public static PendingIntent buildPreferenceButtonPendingIntent(Context context, int appWidgetId) {
		Intent intent = new Intent();
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		intent.setAction("edu.lnu.project.intent.action.PREFERENCES_WEEK"); 
		
	    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	}
	
	/**
	 * Build the update button pending intent
	 * @param context
	 * @return the pending intent
	 */
	public static PendingIntent buildUpdateButtonPendingIntent(Context context, int appWidgetId) {
		Intent intent = new Intent();
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		intent.setAction("edu.lnu.project.intent.action.UPDATE_WEEK"); 
		
	    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	}
	

	public static PendingIntent buildListViewButtonPendingIntent(Context context, int appWidgetId) {
		System.out.println("Create pending intent go to listivew");
		Intent intent = new Intent();
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		intent.setAction("edu.lnu.project.intent.action.GO_TO_LISTVIEW"); 
		
	    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	}
	
	
	/**
	 * Update the widget
	 * @param context
	 * @param remoteViews
	 */
	public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
		ComponentName myWidget = new ComponentName(context, WeekWidgetProvider.class);
	    AppWidgetManager manager = AppWidgetManager.getInstance(context);
	    manager.updateAppWidget(myWidget, remoteViews);		
	}
}
